home *** CD-ROM | disk | FTP | other *** search
/ Java 1996 August / Java - Summer 1996.iso / windows / doc / ui / layout / example / cardwindow.java < prev    next >
Encoding:
Java Source  |  1996-02-26  |  2.5 KB  |  87 lines

  1. /*
  2.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.awt.*;
  18.  
  19. public class CardWindow extends Frame {
  20.     private boolean inAnApplet = true;
  21.      
  22.     Panel cards;
  23.     final static String BUTTONPANEL = "Panel with Buttons";
  24.     final static String TEXTPANEL = "Panel with TextField";
  25.  
  26.     public CardWindow() {
  27.     setLayout(new BorderLayout());
  28.         setFont(new Font("Helvetica", Font.PLAIN, 14));
  29.  
  30.     //Put the Choice in a Panel to get a nicer look.
  31.     Panel cp = new Panel();
  32.     Choice c = new Choice();
  33.     c.addItem(BUTTONPANEL);
  34.     c.addItem(TEXTPANEL);
  35.     cp.add(c);
  36.     add("North", cp);
  37.  
  38.     cards = new Panel();
  39.     cards.setLayout(new CardLayout());
  40.    
  41.     Panel p1 = new Panel();
  42.     p1.add(new Button("Button 1"));
  43.     p1.add(new Button("Button 2"));
  44.     p1.add(new Button("Button 3"));
  45.  
  46.     Panel p2 = new Panel();
  47.     p2.add(new TextField("TextField", 20));
  48.  
  49.     cards.add(BUTTONPANEL, p1);
  50.     cards.add(TEXTPANEL, p2);
  51.     add("Center", cards);
  52.     }
  53.  
  54.     public boolean action(Event evt, Object arg) {
  55.     if (evt.target instanceof Choice) {
  56.         ((CardLayout)cards.getLayout()).show(cards,(String)arg);
  57.         return true;
  58.     }
  59.     return false;
  60.     }
  61.  
  62.     public synchronized boolean handleEvent(Event e) {
  63.         if (e.id == Event.WINDOW_ICONIFY) {//DOESN'T seem to be necessary
  64.             hide(); 
  65.             return true;
  66.         }
  67.         if (e.id == Event.WINDOW_DESTROY) {
  68.             if (inAnApplet) {
  69.                 dispose();
  70.                 return true;
  71.             } else {
  72.                 System.exit(0);
  73.             }
  74.         }   
  75.         return super.handleEvent(e);
  76.     }
  77.  
  78.     public static void main(String args[]) {
  79.     CardWindow window = new CardWindow();
  80.         window.inAnApplet = false;
  81.  
  82.     window.setTitle("CardWindow Application");
  83.     window.pack();
  84.     window.show();
  85.     }
  86. }
  87.